import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        players: JSON.parse(localStorage.player || "[]")
    },
    getters: {
        getIndex: state => item => {
            return state.players.indexOf(item);
        }
    },
    mutations: {
        addPlayer: (state, player) => {
            state.players.push(player);
            localStorage.player = JSON.stringify(state.players);
        },
        editPlater:(state,player)=>{
            state.players[player.index].name = player.name
            state.players[player.index].school = player.school
            localStorage.player = JSON.stringify(state.players);
        },
        delPlayer: (state, item) => {
            const index = state.players.indexOf(item);
            state.players.splice(index, 1);
            localStorage.player = JSON.stringify(state.players);
        }
    },
    actions: {},
    modules: {}
});
